<?php

namespace Admin\Controller;

use Think\Controller;

class IndexController extends Controller {

    function __construct() {
        //继承父类
        parent::__construct();

        //判断登录状态
        if (!D('Admin')->islogin()) {//未登录
            $this->error('您尚未登录，请先登录！', U('login/login'), 3);
        }

        //登录成员的分组
        $group_id = session('admin.role'); //管理员组
        $this->groupinfo = M("Admingroup")->where("group_id=" . $group_id)->field('group_menu')->find();
    }

    //空方法，防止报错
    public function _empty() {
        $this->index();
    }

    //默认首页
    public function index() {
        $this->display();
    }

    //欢迎页
    public function welcome() {
        $this->display();
    }

    //顶部菜单
    public function top() {
        //"menu_type in(1,2,3)"表示仅为菜单形式筛选，操作级别的不出现
        $menuinfo = M("Menuinfo")->where("menu_fatherid=0 and menu_type in(1,2,3) and menu_id in(" . $this->groupinfo['group_menu'] . ")")->order('menu_order desc,menu_id asc')->select();
        $this->assign('menuinfo', $menuinfo);

        $role = session("admin.role");
        $groupinfo = M("Admingroup")->where("group_id=" . $role)->find();
        $this->assign('groupinfo', $groupinfo);

        $this->display();
    }

    //左边菜单
    public function left() {
        $menu_id = I("menu_id", 6); //默认工作台，menu_id=6
        $this->assign('menu_id', $menu_id);

        $this_menuinfo = M("Menuinfo")->where("menu_id=" . $menu_id)->find();
        $this->assign('this_menuinfo', $this_menuinfo);

        //左侧一级菜单
        $menuinfo = M("Menuinfo")->where("menu_fatherid=" . $menu_id . " and menu_type in(1,2,3) and menu_id in(" . $this->groupinfo['group_menu'] . ")")->order('menu_order desc,menu_id asc')->select();
        foreach ($menuinfo as $k => $v) {
            //左侧二级菜单
            $menuinfo[$k]['child'] = M("Menuinfo")->where("menu_fatherid=" . $v['menu_id'] . " and menu_type in(1,2,3) and menu_id in(" . $this->groupinfo['group_menu'] . ")")->order('menu_order desc,menu_id asc')->select();
        }
        $this->assign('menuinfo', $menuinfo);

        //默认选中的菜单menu_id
        $this->assign('active_menu', $menuinfo[0]['child'][0]);

        $this->display();
    }

    //修改密码
    public function passwordadmin() {
        $admin_id = session('admin.admin_id');
        if (IS_POST) {
            $info = I("info");
            $flag = D("Admin")->pswadmin($admin_id, trim($info['oldpassword']), trim($info['newpassword']));
            if ($flag) {
                $this->success('密码修改成功！', U('index/welcome'), 3);
            } else {
                $this->error('密码修改失败！', U('index/passwordadmin'), 3);
            }
        } else {
            $admininfo = M("Admin")->where("admin_id=" . $admin_id)->find();
            $this->assign('admininfo', $admininfo);
            $this->display();
        }
    }

}
